<?php
/**
 * 
 * Build php scripts based on MySql table definitions
 * @author Andy
 *
 */
class caseTool_softwareCreator
{
	private $_baseDir;
	private $_class;
	private $_database;
	private $_meta = array( "" => array ( "" => array ( "" => "" )	));
	private $_table;
	private $_arrTable = array( "" => array ( "" => array ( "" => "" )	));
	
	/**
	 * 
	 * Enter description here ...
	 * @param string $strDatabase
	 * @param string $strTable
	 * @param string $strBaseDir
	 */
	function  __constructor($strDatabase, $strTable, $strBaseDir = "/tmp/sql/")
	{
		
		if(!file_exists($strBaseDir))
		{
			if(!mkdir($strBaseDir))
			{
				throw new Exception("Failed to create ".$strBaseDir);
				return false;
			}
			$this->_baseDir = $strBaseDir;
		}
	
		$this->_set_database($strDatabase);
		$this->_set_table($strTable);
		$this->_set_meta();
	}
	
	/**
	 * 
	 * Set class file name
	 */
	private function _set_class()
	{
		$this->_class = "database_".$this->_table.".class.php";	
		
	}
	
	/**
	 * 
	 * Set database name
	 * @param string $strDatabase
	 */
	private function _set_database($strDatabase)
	{
		$this->_database = $strDatabase;
	}
	
	/**
	 * 
	 * Get table meta data and load into $_meta array
	 * 
	 * @throws Exception
	 */
	private function _set_meta() 
	{
		$strSql = "SHOW * FROM " . $this->table . " LIMIT 1";
		$qryFields = new MySqlResults( $strSql );
		if ($qryFields->result) 
		{
			$iField = mysql_num_fields ( $qryFields->result );
			$iLoop = 0;
			while ( $iLoop < $iField ) 
			{
				$meta = mysql_fetch_field ( $qryFields->result, $iLoop );
				$this->meta [$meta->name] = $meta;
				$iLoop ++;
			}
		}else 
			throw new Exception("Unable to load meta data");
	}
	
	/**
	 * 
	 * Check table exists and set $_table
	 * @param string $strTable
	 * @throws Exception
	 */
	private function _set_table($strTable)
	{
		$strSql =  "SHOW TABLES 
					FROM ".$this->_database;
		
		$qryTable = new MySqlResults($strSql);

		do{
			if($strTable == $qryTable->myrow[0])
			{
				$this->_table = $strTable;
				return true;
			}
		}while($qryTable->myrow = mysql_fetch_array($qryTable->result));
		
		throw new Exception($this->_database.".".$strTable." doesn't exist");
	}
	
	/**
	 * 
	 * Enter description here ...
	 */
	private function _set_arrTable()
	{
		$strSql =  "SHOW TABLES 
					FROM ".$this->_database;
		
		$qryTable = new MySqlResults($strSql);
		//print_array($qryTables->myrow);
		//echo $qryTables->myrow[0];
		//print_object($qryTables);
		
	}
	
	private function _createClass()
	{
		/* Open file */
		$this->_set_class();
		
		//$this->_meta['name'];
		/* loop through fields */
		foreach ($this->_meta as $type=>$data)
		{
			$txtBody = <<<EOF
			public function get_{$this->_meta['name']}()
			{
				if(isset($this->{$this->_meta['name']}['value']))
					return $this->{$this->_meta['name']}['value'];
				else
					return false;
			}

			
EOF;

		}
	}
}